package class02;

import javax.print.attribute.standard.Sides;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class RingArray2 {
    public static class MyQueue {
        private int[] arr;
        private int pushi;
        private int popi;
        private int size;
        private final int limit;

        public MyQueue(int limit) {
            arr = new int[limit];
            pushi = 0;
            popi = 0;
            size = 0;
            this.limit = limit;
        }

        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("队列满了，不能再放了!");
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }

        public int pop() {
            if (size == 0) {
                throw new RuntimeException("队列为空，不能取了！");
            }
            size--;
            int ans = arr[popi];
            popi = nextIndex(popi);
            return ans;
        }

        public boolean isEqual() {
            return size == 0;
        }

        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1: 0;
        }
    }
}
